/**
 * 
 */
package sqlToGUI.charts;

import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CompassPlot;
import org.jfree.data.general.DefaultValueDataset;

import sqlToGUI.charts.helperUtilities.TelemetryChartEvent;
import sqlToGUI.charts.helperUtilities.TelemetryChartListener;
import sqlToGUI.charts.helperUtilities.TelemetryDataEntry;



/**
 * @author Maciej
 *
 */
public class WindChart implements GUIDisplay {

	/**The list receiving the new data from the outside -
	 * This class will be responsible for listening to this
	 * list and updating from it whenever new information
	 * is available -
	 * The array is used in case chart uses reference to more
	 * than one data set and is updated from more than one
	 * list */
	private TelemetryDataEntry [] dataEntries;
	
	private DefaultValueDataset buffer;	
	private JFreeChart windDirection;
	private ChartPanel panel;
	
	/**The log panel data will monitor the incoming data */
	private LogPanel log;
	/**Variable indicating whether or not a log has been
	 * registered */
	private boolean logReg = false;

	/**
	 * 
	 */
	public WindChart() {
		super();
		
		buffer = new DefaultValueDataset(50);

		CompassPlot plot = new CompassPlot(buffer);
		plot.setSeriesNeedle(7);
		plot.setSeriesPaint(0, Color.black);
		plot.setSeriesOutlinePaint(0, Color.black);
		plot.setRosePaint(Color.red);
		plot.setRoseHighlightPaint(Color.gray);
		plot.setRoseCenterPaint(Color.white);
		plot.setDrawBorder(false);
		plot.setLabelType(CompassPlot.NO_LABELS);
		plot.setLabelFont(CompassPlot.DEFAULT_LABEL_FONT);
		
		
		float h = 500;//displayFrame.getHeight();

		//float w = 500;//displayFrame.getWidth();

        // GradientPaint gradientPaint = new GradientPaint(0.0F, 10.0F, Color.WHITE, h, w, Color.green.darker());

        //plot.setBackgroundPaint(gradientPaint);
					
		windDirection = new JFreeChart(plot);
		Image img = null;
		try {
			img = ImageIO.read(new File("backgrounds/windchart/compass.gif"));
		} catch (IOException e) {
		}

        windDirection.setBackgroundImage(img);
        		//new GradientPaint(0,0,Color.lightGray,0,h, Color.blue));
        
		panel = new ChartPanel(windDirection);
	}
	
	
	/* (non-Javadoc)
	 * @see sqlToGUI.charts.GUIDisplay#getChart()
	 */
	public Container getChart() {
		return panel;
	}
		
	/* (non-Javadoc)
	 * @see sqlToGUI.charts.GUIDisplay#addToBuffer(int, double)
	 */
	public void addToBuffer(int index, double value) {
		buffer.setValue(value);
	}

	/**Register the given log so that it can monitor incoming data */
	public GUIDisplay registerLog(LogPanel log){
		this.log = log;
		logReg = true;
		return this;
	}

	@Override
	/**Return the buffer for the chart */
	public TelemetryDataEntry [] getBuffers() {
		
		dataEntries = new TelemetryDataEntry [1];
		for(int i = 0; i < dataEntries.length; i++)
			dataEntries[i] = new TelemetryDataEntry(1);

		/**Create the event(update chart) when this buffer
		 * receives new data */
		dataEntries[0].addTelemetryChartEventListener(
				new TelemetryChartListener() {
			public void myEventOccurred(TelemetryChartEvent evt) {
				addToBuffer(0, dataEntries[0].getUpdate()[0]);
			}
		});

		/**TODO: send data to the log*/
		
		return dataEntries;
	}
}
